// Created by WXX on 2021/9/10 10:06
#include "MyLinkedList.h"

/**
 * 执行用时：0 ms, 在所有 C++ 提交中击败了100.00%的用户
 * 内存消耗：6.9 MB, 在所有 C++ 提交中击败了38.77%的用户
 */
class Solution {
public:
    ListNode* middleNode(ListNode* head) {
        int cnt = 0;
        auto p = head;
        while (p) {
            cnt++;
            p = p->next;
        }
        p = head;
        for (int i = 0; i < cnt / 2; i++) p = p->next;
        return p;
    }
};

int main() {

    ListNode *head = MyLinkedList({1, 2, 3, 4, 5}).getHead();
    MyLinkedList().printLinkedList(Solution().middleNode(head));

    return 0;
}
